package com.kevin.spider.rocketmq.service;

import jakarta.annotation.Resource;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

/**
 * @author 张帆
 * @date: 2025/4/17 10:01
 */
@Service
public class RocketMQProduceService {
    @Resource
    private RocketMQTemplate rocketMQTemplate;

    /**
     * 异步发送消息
     *
     * @param topic
     * @param msg
     */
    public void asyncSend(String topic, Object msg) {
        SendResult sendResult = rocketMQTemplate.syncSend(topic, msg);
        Assert.isTrue(sendResult.getSendStatus() == SendStatus.SEND_OK, "消息发送失败");
    }
}
